export function cronChangeDate(str) {
  // cron表达式反解析时间
  // 19 30 16 * * 2,3 * 按周
  // 44 30 16 21 * ? * 按月
  var toDate = {};
  if (!str) {
    return;
  } else {
    var result = str.split(" ").join("");
    var nArr = str.split(" ");
    var countData = getPlaceholderCount(result);
    var str1 = "";
    str1 = result.split("").reverse().join("").substring(3, 4);
    if (countData.count1 && str1 == "?") {
      // 没有'?'则是按周循环
      toDate.loopType = "周循环";
      var keys = nArr[5];
      var en2cnMap = {
        1: "星期日",
        2: "星期一",
        3: "星期二",
        4: "星期三",
        5: "星期四",
        6: "星期五",
        7: "星期六",
      };
      var cnKeys = keys.split(",").map(function (key, idx) {
        return en2cnMap[key];
      });
      toDate.loopValue = cnKeys.join(",");
    } else if (countData.count1 + countData.count2 === 3) {
      toDate.loopType = "月循环";
      var mot = [];
      var mkeys = nArr[3].split(",");
      for (var i = 0; i < mkeys.length; i++) {
        let mo = mkeys[i];
        mot.push(mo);
      }
      toDate.loopValue = mot.join(",");
    } else {
    }
    toDate.loopTime =
      nArr[6] +
      "-" +
      nArr[4] +
      "-" +
      nArr[3] +
      " " +
      nArr[2] +
      ":" +
      nArr[1] +
      ":" +
      nArr[0];
  }
  //console.log(toDate, "反解析");
  return toDate;
}
// 统计字符串中包含某个字符的个数
function getPlaceholderCount(strSource) {
  var count1 = 0; // ?的个数
  var count2 = 0; // *的个数
  strSource.replace(/\*|\?/g, function (m, i) {
    if (m === "?") {
      count1++;
    } else if (m === "*") {
      count2++;
    }
  });

  var obj = {};
  obj.count1 = count1;
  obj.count2 = count2;
  return obj; //返回一个对象，根据需要得到想要的值
}

export function dateChangeCron(date, isTime) {
  let cron = "";
  let m = "";
  let s = "";
  let h = "";
  let year = "";
  let month = "";
  let day = "";
  let date0 = new Date(date)
  h = date0.getHours()
  m = date0.getMinutes()
  s = date0.getSeconds()
  if (isTime) {
    year = '*'
    month = '*'
    day = "*"
  }else{
    year = date0.getFullYear()
    month = date0.getMonth() + 1 < 10
    ? "0" + (date0.getMonth() + 1)
    : date0.getMonth() + 1
    day = date0.getDate() < 10
    ? "0" + date0.getDate()
    : date0.getDate()
  }
  cron =
    s +
    " " +
    m +
    " " +
    h +
    " " +
    day +
    " " +
    month +
    " " +
    "?" +
    " " +
    year;
  return cron
}